package tk.winshu.transaction.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.winshu.transaction.entity.User;
import tk.winshu.transaction.repository.IUserRepository;

/**
 * @author Kevin
 * @since 2019/04/06.
 */
@Service
public class UserService {

    private static final String PREFIX = "user_";

    private final IUserRepository repository;

    @Autowired
    public UserService(IUserRepository repository) {
        this.repository = repository;
    }

    private void create(int index) {
        repository.save(new User(PREFIX + index, PREFIX + index));
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void createWithRequired(int index) {
        create(index);
    }

    @Transactional(propagation = Propagation.NESTED)
    public void createWithNested(int index) {
        create(index);
    }

}
